package com.common.android.utils;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

/**
 * 应用包相关工具类
 * <br>Created 2014-8-22 下午4:59:40
 * @version  
 * @author   huangyx		
 * @see 	 
 * Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
 *
 */
public class PackageUtils {
    
    /**
     * 判断应用程序是否位于堆栈的顶层
     * <br>Created 2014-8-27 上午10:01:43
     * @param isOpenCamera 是否打开相机
     * @return 应用程序位于堆栈的顶层返回true，否则返回false
     * @author       huangyx
     */
	public static boolean isTopActivity(Context context, boolean isOpenCamera) {
		if(isOpenCamera){
			return true;
		}
		String packageName = context.getPackageName();
		ActivityManager activityManager = (ActivityManager) context
		        .getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
		if (tasksInfo.size() > 0) {
			// 应用程序位于堆栈的顶层
			String topActivityPackageName = tasksInfo.get(0).topActivity.getPackageName(); 
			if (packageName.equals(topActivityPackageName)
			        || "com.android.camera".equals(topActivityPackageName)) {
				return true;
			}
		}
		return false;
	}

	/**
     * 判断某个activity是否在显示
     * <br>Created 2014-5-22 下午7:55:09
     * @param target 要判断的Activity类
     * @return 该activity是在显示返回true，否则返回false
     * @author       cb
    */
    public static boolean isTopActivity(Context context, Class<?> target) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
        if (tasksInfo.size() > 0) {
            // 应用程序位于堆栈的顶层
            String acti = tasksInfo.get(0).topActivity.getClassName();
            String tar = target.getName();
            if (acti.equals(tar)){
                return true;
            }
        }
        return false;
    }
    
    /**
     * 判断应用的包名是否是指定的包名
     * <br>Created 2014-8-22 下午5:20:10
     * @param mContext 上下文
     * @param packageName 包名
     * @return 应用的包名是是指定的包名返回true，否则返回false
     * @author       huangyx
     */
	public static boolean hasPackageName(Context mContext ,String packageName){
		PackageInfo packageInfo;
		try {
			packageInfo = mContext.getPackageManager()
			        .getPackageInfo(packageName,0);
		} catch (NameNotFoundException e) {
			packageInfo = null;
			e.printStackTrace();
		}
		return !(packageInfo == null);
    }
	
	/**
	 * 应用跳转
	 * <br>Created 2014-8-22 下午5:22:53
	 * @param context 上下文
	 * @param packageName 应用包名
	 * @param classname 应用类名
	 * @param appname 应用名
	 * @return 跳转成功返回true，否则返回false
	 * @author       huangyx
	 */
	public static boolean jumpPackage(Context context, String packageName, 
	        String classname, String appname){
		if(hasPackageName(context, packageName)){
			jumpActivity(context, packageName);
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 应用跳转
     * <br>Created 2014-8-22 下午5:25:53
     * @param context 上下文
     * @param packageName 应用包名
	 * @author       huangyx
	 */
	public static void jumpActivity(Context context,String packageName){
		Intent intent=context.getPackageManager().getLaunchIntentForPackage(packageName);
		try {
			context.getPackageManager().getPackageInfo(packageName, 
			        PackageManager.GET_ACTIVITIES);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		if(intent != null) {
			context.startActivity(intent);
		}
	}
	
}